package com.ztao.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.model.common.admin.dto.NewsAuthDto;
import com.heima.model.common.apArticle.dto.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.pojos.WmChannel;
import com.heima.model.common.wemedia.pojos.WmNews;
import com.heima.model.common.wemedia.pojos.WmUser;
import com.heima.utils.aliyun.TextModerationPlusUtil;
import com.ztao.api.feignarticle.ArticleClientApi;
import com.ztao.mapper.WmChannelMapper;
import com.ztao.mapper.WmNewsMapper;
import com.ztao.mapper.WmUserMapper;
import com.ztao.service.WmNewsAutoScanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {
    @Resource
    private WmNewsMapper wmNewsMapper;

    /**
     * 审核
     *
     * @param id
     */
    @Resource
    private WmChannelMapper channelMapper;
    @Resource
    private WmUserMapper wmUserMapper;
    @Resource
    private ArticleClientApi articleClientApi;
    @Override
    public void autoScanWnMews(Integer id) {
        WmNews wmNews=this.wmNewsMapper.selectById(id);
        if(wmNews!=null){
            if(wmNews.getStatus()==4){
                ArticleDto articleDto=new ArticleDto();
                BeanUtils.copyProperties(wmNews,articleDto);
                articleDto.setId(wmNews.getArticleId());

                WmChannel wmChannel = this.channelMapper.selectById(wmNews.getChannelId());
                if(wmChannel!=null){
                    articleDto.setChannelName(wmChannel.getName());
                }
                WmUser wmUser = this.wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());
                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setLayout(wmNews.getType());
                ResponseResult result1 = this.articleClientApi.saveArticle(articleDto);
                if(result1.getCode()==200){//同步成功
                    //进行关联
                    wmNews.setArticleId((Long) result1.getData());
                    //是否发布
                    Date date = wmNews.getPublishTime();
                    if(!date.after(new Date())){
                        //发布 修改状态为9
                        wmNews.setStatus((short) 9);
                    }
                    this.wmNewsMapper.updateById(wmNews);
                }
                return ;
            }
            //获取内容
            String title = wmNews.getTitle();
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(title);
            List<Map> maps = JSON.parseArray(wmNews.getContent(), Map.class);
            for (Map map : maps) {
                //传递过来是个图片
                if(map.get("type").equals("text")){
                    //把获取到的图片保存到images里面 -list集合
                    stringBuilder.append((String) map.get("value"));
                }
            }
            ResponseResult result = TextModerationPlusUtil.checkContent(stringBuilder.toString());
            if (result.getCode() == 600) {//审核通过
                wmNews.setStatus((short) 8);
                this.wmNewsMapper.updateById(wmNews);
                //审核通过之后--》需要同步信息

                ArticleDto articleDto=new ArticleDto();
                BeanUtils.copyProperties(wmNews,articleDto);
                articleDto.setId(wmNews.getArticleId());

                WmChannel wmChannel = this.channelMapper.selectById(wmNews.getChannelId());
                if(wmChannel!=null){
                    articleDto.setChannelName(wmChannel.getName());
                }
                WmUser wmUser = this.wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());
                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setLayout(wmNews.getType());
                ResponseResult result1 = this.articleClientApi.saveArticle(articleDto);
                if(result1.getCode()==200){//同步成功
                    //进行关联
                    wmNews.setArticleId((Long) result1.getData());
                    //是否发布
                    Date date = wmNews.getPublishTime();
                    if(!date.after(new Date())){
                        //发布 修改状态为9
                        wmNews.setStatus((short) 9);
                    }
                    this.wmNewsMapper.updateById(wmNews);
                }


            }else if(result.getCode()==602) {//转人工
                wmNews.setStatus((short) 3);
                this.wmNewsMapper.updateById(wmNews);
            }else {//审核拒绝
                    wmNews.setStatus((short) 2);
                    wmNews.setReason(result.getErrorMessage());
                    this.wmNewsMapper.updateById(wmNews);
                }
            }
        }

}

